package com.huawei.fastengine.fastview.download.install;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.content.pm.PackageInstaller.Session;
import android.content.pm.PackageInstaller.SessionParams;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PackageParser;
import android.content.pm.PackageParser.PackageParserException;
import android.util.Log;
import androidx.core.content.FileProvider;
import com.huawei.fastengine.fastview.download.utils.log.FastViewLogUtils;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public abstract class InstallProcessSdk24 {
    public static final int INSTALL_FAILED = 0;
    public static final int NO_SUPPORT = -1;
    public static final int SUPPORT = 1;
    private static final String TAG = "InstallProcessSdk24";
    private static int hiappUid = -1;

    private static void closeIO(InputStream inputStream, OutputStream outputStream) {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                FastViewLogUtils.e(TAG, e.toString());
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e2) {
                FastViewLogUtils.e(TAG, e2.toString());
            }
        }
    }

    public static int getApplicationUid(Context context) {
        if (hiappUid == -1) {
            try {
                hiappUid = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0).uid;
            } catch (NameNotFoundException e) {
                Log.e(TAG, "can not get uid NameNotFoundException");
            }
        }
        return hiappUid;
    }

    public static Intent getNomalInstallIntent(Context context, String str) {
        Intent data = new Intent("android.intent.action.INSTALL_PACKAGE").setData(FileProvider.getUriForFile(context, context.getPackageName() + ".fastapp.engine.fileProvider", new File(str)));
        if (!(context instanceof Activity)) {
            data.addFlags(268435456);
        }
        data.addFlags(1);
        return data;
    }

    @TargetApi(24)
    public static int installPackage(Context context, String str, String str2) {
        OutputStream outputStream;
        Session session;
        InputStream inputStream;
        Throwable th;
        Throwable th2;
        SessionParams sessionParams = new SessionParams(1);
        sessionParams.setOriginatingUid(getApplicationUid(context));
        File file = new File(str2);
        try {
            sessionParams.setInstallLocation(PackageParser.parsePackageLite(file, 0).installLocation);
        } catch (PackageParserException e) {
            Log.e(TAG, "Cannot parse package PackageParserException");
        }
        PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
        Session session2 = null;
        try {
            int createSession = packageInstaller.createSession(sessionParams);
            byte[] bArr = new byte[WXMediaMessage.THUMB_LENGTH_LIMIT];
            session2 = packageInstaller.openSession(createSession);
            try {
                InputStream fileInputStream = new FileInputStream(file);
                try {
                    long length = file.length();
                    OutputStream openWrite = session2.openWrite(context.getPackageName(), 0, length);
                    while (true) {
                        try {
                            int read = fileInputStream.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            openWrite.write(bArr, 0, read);
                            if (length > 0) {
                                session2.setStagingProgress(((float) read) / ((float) length));
                            }
                        } catch (IOException e2) {
                            outputStream = null;
                            inputStream = null;
                            session = session2;
                        } catch (Throwable th3) {
                            th2 = th3;
                            outputStream = null;
                            inputStream = null;
                            session = session2;
                            th = th2;
                            closeIO(inputStream, outputStream);
                            if (session != null) {
                                session.close();
                            }
                            throw th;
                        }
                    }
                    session2.fsync(openWrite);
                    closeIO(fileInputStream, openWrite);
                    inputStream = null;
                    outputStream = null;
                    try {
                        Intent intent = new Intent(context.getPackageName() + ".install.ACTION_INSTALL_COMMIT");
                        intent.putExtra("install_result_receiver_packagename", str);
                        intent.setPackage(context.getPackageName());
                        PendingIntent broadcast = PendingIntent.getBroadcast(context, createSession, intent, HwAccountConstants.FLAG_TRANS_NAVIGATION_BAR);
                        if (broadcast != null) {
                            session2.commit(broadcast.getIntentSender());
                        }
                        closeIO(null, null);
                        if (session2 != null) {
                            session2.close();
                        }
                        return 1;
                    } catch (IOException e3) {
                        session = session2;
                        Log.e(TAG, "installPackage IOException");
                        closeIO(inputStream, outputStream);
                        if (session == null) {
                            return 0;
                        }
                        session.close();
                        return 0;
                    } catch (Throwable th4) {
                        th2 = th4;
                        session = session2;
                        th = th2;
                        closeIO(inputStream, outputStream);
                        if (session != null) {
                            session.close();
                        }
                        throw th;
                    }
                } catch (IOException e4) {
                    outputStream = null;
                    inputStream = fileInputStream;
                    session = session2;
                    Log.e(TAG, "installPackage IOException");
                    closeIO(inputStream, outputStream);
                    if (session == null) {
                        return 0;
                    }
                    session.close();
                    return 0;
                } catch (Throwable th32) {
                    inputStream = fileInputStream;
                    session = session2;
                    th = th32;
                    outputStream = null;
                    closeIO(inputStream, outputStream);
                    if (session != null) {
                        session.close();
                    }
                    throw th;
                }
            } catch (IOException e5) {
                inputStream = null;
                session = session2;
                outputStream = null;
                Log.e(TAG, "installPackage IOException");
                closeIO(inputStream, outputStream);
                if (session == null) {
                    return 0;
                }
                session.close();
                return 0;
            } catch (Throwable th5) {
                session = session2;
                th = th5;
                inputStream = null;
                outputStream = null;
                closeIO(inputStream, outputStream);
                if (session != null) {
                    session.close();
                }
                throw th;
            }
        } catch (IOException e6) {
            inputStream = null;
            session = session2;
            outputStream = null;
            Log.e(TAG, "installPackage IOException");
            closeIO(inputStream, outputStream);
            if (session == null) {
                return 0;
            }
            session.close();
            return 0;
        } catch (Throwable th52) {
            session = session2;
            th = th52;
            inputStream = null;
            outputStream = null;
            closeIO(inputStream, outputStream);
            if (session != null) {
                session.close();
            }
            throw th;
        }
    }
}
